package de.koller.aframe.util.math;

public class SphereUtil implements IFunction2D {
	
	/** radius = 1! */
	public static final IFunction2D INSTANCE = new SphereUtil();
	
	private SphereUtil() {}
	
	@Override
	public float getValue( float x, float y ) {
		float c = x*x + y*y - 1;
		
		float disc = -4 * c;
		if( disc < 0 )
			return 0;

		float distSqrt = (float)Math.sqrt( disc );
		float q = distSqrt / 2f;

		float t0 = q;
		float t1 = c / q;

		if( t0 > t1 ) {
			float temp = t0;
			t0 = t1;
			t1 = temp;
		}

		if( t1 < 0 )
			return 0;

		if( t0 < 0 )
			return t1;
		else
			return t0;
	}
	
}
